package com.qf.business.goods.controller;

import com.qf.business.goods.service.GoodsNormsService;
import com.qf.business.goods.service.GoodsNormsValService;
import com.qf.business.goods.service.GoodsSortNormsService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.goods.entity.GoodsNorms;
import com.qf.data.goods.entity.GoodsNormsVal;
import com.qf.data.goods.entity.GoodsSortNorms;
import com.qf.data.goods.vo.input.GoodsNormsInput;
import com.qf.data.goods.vo.output.NormsOutput;
import com.qf.data.goods.vo.output.NormsSelectOutput;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@RestController
@RequestMapping("/norms")
@Slf4j
public class NormsController {

    @Autowired
    private GoodsNormsService goodsNormsService;

    @Autowired
    private GoodsNormsValService goodsNormsValService;

    @Autowired
    private GoodsSortNormsService goodsSortNormsService;

    /**
     * 查询规格列表
     * @return
     */
    @RequestMapping("/list")
    public R list(){

        List<NormsOutput> normsOutputs = goodsNormsService.queryList();
        return Rs.create(normsOutputs);
    }

    @RequestMapping("/select/list")
    public R selectlist(){
        List<GoodsNorms> norms = goodsNormsService.list();
        //将原本的规格实体 通过stream流 逐个的转换成   对象 生成一个集合返回客户端
        List<NormsSelectOutput> selectList = norms.stream().map(goodsNorms ->{
            NormsSelectOutput normsSelectOutput = new NormsSelectOutput();
            normsSelectOutput.setKey(goodsNorms.getId());
            normsSelectOutput.setLabel(goodsNorms.getNormsName());
            return normsSelectOutput;
        }).collect(Collectors.toList());


        return Rs.create(selectList);
    }

    /**
     * 查询指定分类下，有个规格id集合
     * @param normsId
     * @return
     */
    @RequestMapping("/list/vals")
    public R listVal(Long normsId){
        List<GoodsNormsVal> goodsNormsVals = goodsNormsService.queryListVal(normsId);
        return Rs.create(goodsNormsVals);
    }

    @RequestMapping("/has/list")
    public R querySortHasNorms(Long sortId){
        List<GoodsSortNorms> hasNorms = goodsSortNormsService.query().eq("sort_id", sortId).list();
        List<Long> normsIds = hasNorms.stream()
                .map(goodsSortNorms -> goodsSortNorms.getNormsId())
                .collect(Collectors.toList());
        return Rs.create(normsIds);
    }

    /**
     * 新增规格
     * @param input
     * @return
     */
    @RequestMapping("/insert")
    public R insert(@Valid GoodsNormsInput input){
        GoodsNorms goodsNorms = QfBeanUtils.beanCopy(input, GoodsNorms.class);
        goodsNormsService.save(goodsNorms);
        return Rs.create(true);
    }

    /**
     * 新增规格数据
     * @return
     */
    @RequestMapping("/insert/vals")
    public R insertVal(Long normsId,String normsVal){
        GoodsNormsVal goodsNormsVal = new GoodsNormsVal();
        goodsNormsVal.setNormsId(normsId);
        goodsNormsVal.setNormsVal(normsVal);
        goodsNormsValService.save(goodsNormsVal);
        return Rs.create(true);
    }

    @RequestMapping("/listAndVals")
    public R listAndVals(Long sid){
        log.info("[norms vals query] - 根据分类查询规格以及规格值的列表 - {}",sid);
        List<NormsOutput> normsOutputs = goodsNormsService.queryListBySid(sid);
        return Rs.create(normsOutputs);
    }

}
